#include "losu.h"
#include "losu_bytecode.h"
#include "losu_dump.h"
#include "losu_gc.h"
#include "losu_mem.h"
#include "losu_object.h"
#include "losu_syntax.h"
#include "losu_vm.h"

#include "queue.h"
#include "../xkit.h"
#include "collections.h"

/** losu-capi (self, size: number) -> self
 * @brief: "constructor of class: THISxkit_MODULEcollections_CLASSQueue"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_constructor(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    QueueHeader *h = NULL;
    size_t size = obj_getnum(vm, argv + 1);
    h = QueueInit(vm, size);
    if (h) {
        obj_unitsetstr(vm, *argv, "__cptr__", obj_newptr(vm, h));
    }
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> number
 * @brief: "返回当前元素数量"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_FUNCsize(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    QueueHeader *h = getCPointer(vm, argv);
    return arg_returnNum(vm, QueueSize(vm, h));
}


/** losu-capi (self, item: any) -> self
 * @brief: "将元素加入队尾"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_FUNCenqueue(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    QueueHeader *h = getCPointer(vm, argv);
    QueueEnqueue(vm, h, argv+1);
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> any
 * @brief: "删除并返回队头元素"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_FUNCdequeue(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    QueueHeader *h = getCPointer(vm, argv);
    losu_object target;
    QueueDequeue(vm, h, &target);
    stack_push(vm, target);
    return 1;
}


/** losu-capi (self) -> 
 * @brief: "析构函数"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_FUNC__del__(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    QueueHeader *h = getCPointer(vm, argv);
    QueueFree(vm, h);
    obj_unitsetstr(vm, *argv, "__cptr__", obj_newfalse(vm));
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> any
 * @brief: "返回队头元素但不删除"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_FUNCpeek(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    QueueHeader *h = getCPointer(vm, argv);
    losu_object target;
    QueuePeek(vm, h, &target);
    stack_push(vm, target);
    return 1;
}


/** losu-capi (self) -> bool
 * @brief: "检查队列是否满"
 */
int32_t THISxkit_MODULEcollections_CLASSQueue_FUNCisFull(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    QueueHeader *h = getCPointer(vm, argv);
    return QueueFull(vm, h) ? arg_returnTrue(vm) : arg_returnFalse(vm);
}
